
'''根据生成的结果处理正确率, 从top1~top10'''

def calculate_top_K(predict, label, K):
	topK = predict[:K+1]      # 取出前K个预测元素, 注意后面是开区间, 所以要加1
	if label in topK:
		return True
	return False


def calculate_tops(file_path):

	n_categorized_correctly = [0 for _ in range(10)]    # 分别代表top1, top2, ...., top10的正确数

	with open(file_path, 'r') as reader:
		samples = reader.readlines()
		n_samples = len(samples)     # 样本总数
		for line in samples:
			temp = line.strip().split(',')
			predict = temp[0].strip().split(' ')
			# print(len(predict))
			label = temp[1]

			for k in range(10):
				if calculate_top_K(predict, label, k):
					n_categorized_correctly[k] += 1
	# print(n_categorized_correctly)
	topK_accs = list(map(lambda x: x/n_samples, n_categorized_correctly))
	return topK_accs

def ttss():
	# for i in range(10):
	for i in [9]:
		file_path = '../data/custom/svm/predict_窗口{}_LDASVM_T600_N300_a0.08333333333333333_b0.1.csv'.format(i)
		# file_path = '../data/custom/KL/predict_窗口{}_LDAKL_T600_N300_a0.16666666666666666_b0.1.csv'.format(i)
		# file_path = '../data/custom/DERTOM/predict_窗口{}_DERTOM_T600_N300_a0.01_b0.01.csv'.format(i)
		topK_accs = calculate_tops(file_path)
		print('{}\t{}'.format('%.3f' % topK_accs[0], '%.3f' % topK_accs[4]))

if __name__ == '__main__':
	# file_path = '../data/two/predict_eclipse_T300_N100_a0.1_b0.1.csv'
	# file_path = '../data/custom/svm/predict_窗口3_LDASVM_T300_N100_a0.1_b0.1.csv'
	# file_path = '/home/wanglinhui/predict.txt'
	# print(file_path)
	# topK_accs = calculate_tops(file_path)
	# for i in range(10):
	# 	print('%.5f' % topK_accs[i])
	# print(topK_accs)
	# print('%.3f' % topK_accs[0])
	# print('%.3f' % topK_accs[4])
	# ttss()